package cn.walls1717.mall.modules.pms.controller;


import cn.walls1717.mall.common.api.CommonPage;
import cn.walls1717.mall.common.api.CommonResult;
import cn.walls1717.mall.common.util.ComResultUtil;
import cn.walls1717.mall.modules.pms.dto.PageListDTO;
import cn.walls1717.mall.modules.pms.dto.RelationAttrInfoDTO;
import cn.walls1717.mall.modules.pms.model.PmsProductAttribute;
import cn.walls1717.mall.modules.pms.model.PmsProductAttributeCategory;
import cn.walls1717.mall.modules.pms.service.PmsProductAttributeCategoryService;
import cn.walls1717.mall.modules.pms.service.PmsProductAttributeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author walls1717
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/productAttributes")
public class PmsProductAttributeController {

    private final PmsProductAttributeService attributeService;

    @Autowired
    public PmsProductAttributeController(PmsProductAttributeService attributeService) {
        this.attributeService = attributeService;
    }

    @ApiOperation("商品属性规格或参数列表")
    @GetMapping("list/{cid}")
    public CommonResult<CommonPage<PmsProductAttribute>> pageList(@Valid PageListDTO pageListDTO) {
        Page<PmsProductAttribute> list = attributeService.pageList(pageListDTO);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("商品属性添加")
    @PostMapping()
    public CommonResult<?> createAttribute(@RequestBody PmsProductAttribute pmsProductAttribute) {
        boolean result = attributeService.createAttribute(pmsProductAttribute);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品属性修改")
    @PutMapping("{id}")
    public CommonResult<?> updateAttribute(@RequestBody PmsProductAttribute pmsProductAttribute) {
        boolean result = attributeService.updateById(pmsProductAttribute);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品属性删除")
    @DeleteMapping()
    public CommonResult<?> delete(@RequestParam("ids") List<Long> ids) {
        boolean result = attributeService.delete(ids);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品属性编辑页面数据初始化")
    @GetMapping("{id}")
    public CommonResult<PmsProductAttribute> getOneAttribute(@PathVariable Integer id) {
        PmsProductAttribute byId = attributeService.getById(id);
        return CommonResult.success(byId);
    }

    @ApiOperation("编辑页面展示筛选属性")
    @GetMapping(path = "attrInfo/{productCategoryId}")
    public CommonResult<List<RelationAttrInfoDTO>> getAttrInfo(@PathVariable Integer productCategoryId) {
        List<RelationAttrInfoDTO> list = attributeService.getAttrInfo(productCategoryId);
        return CommonResult.success(list);
    }

}

